/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package convertir;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java_cup.runtime.Symbol;

/**
 *
 * @author Sheily
 */
public class Ejecutar {

    public final static int GENERAR = 1;
    public final static int EJECUTAR = 2;
    public final static int SALIR = 3;

    public static void main(String[] args) throws IOException, Exception {
        java.util.Scanner in = new Scanner(System.in);
        int valor = 0;
        do {
            System.out.println("Elija una opcion: ");
            System.out.println("1) Generar");
            System.out.println("2) Ejecutar");
            System.out.print("Opcion: ");
            valor = in.nextInt();
            switch (valor) {
                case GENERAR: {

                    String archLexico = "";
                    String archSintactico = "";
                    if (args.length > 0) {
                        archLexico = args[0];
                        archSintactico = args[0];
                    } else {
                        archLexico = "lexer.flex";
                        archSintactico = "sintactico.cup";
                    }
                    String[] alexico = {archLexico};
                    jflex.Main.main(alexico);
                    String[] sintactico = {"-parser", "AnalizadorSintactico", archSintactico};
                    java_cup.Main.main(sintactico);

                    //movemos los archivos generados
                    boolean mvAL = moverArch("AnalizadorLexico.java");
                    boolean mvAS = moverArch("AnalizadorSintactico.java");
                    boolean mvSym = moverArch("sym.java");
                    if (mvAL && mvSym && mvAS) {
                        System.exit(0);
                    }
                    break;
                }
                case EJECUTAR: {
                    
                    String archivo[] = {"C:/Users/Sheily/Documents/NetBeansProjects/JFlex_Convertir/src/convertir/entrada.txt"};
                    AnalizadorSintactico.main(archivo);
                }
                default: {
                    System.out.println("Opcion no valida!");
                    break;
                }
            }
        } while (valor != 3);
    }

    public static boolean moverArch(String archNombre) {
        boolean efectuado = false;
        File arch = new File(archNombre);
        if (arch.exists()) {
            Path currentRelativePath = Paths.get("");
            String nuevoDir = currentRelativePath.toAbsolutePath().toString()
                    + File.separator + "src" + File.separator
                    + "convertir" + File.separator + arch.getName();
            File archViejo = new File(nuevoDir);
            archViejo.delete();
            if (arch.renameTo(new File(nuevoDir))) {
                efectuado = true;
            }
        } else {
            System.out.println("\n*** Codigo no existente ***\n");
        }
        return efectuado;
    }
}
